<script>
export default {
	data(){
		return{
			dialogCom: false,
			tipTxt: '',
			statusData: [
				{ idClass: 'back-error', label: 1, title: '已经取号'},
				{ idClass: 'back-success', label: 2, title: '预约成功'},
				{ idClass: 'back-edit', label: 3, title: '已取消'},
				{ idClass: 'back-overtime', label: 4, title: '超时未取号'},
			], 
			img1:'../../static/newImg/warn.png',
			img2:'../../static/newImg/nullData.png',
			historyList: [],
			submitFlag: true,
			userInfo: uni.getStorageSync("userInfo"),
			type: null,
			item: null
		}
	},
	onLoad(obj) {
		this.type = obj.type || 2;
	},
	onShow() {
		this.getHistoryList()
		this.getLocationc();
	},
	methods: {
		getLocationc(){
			let that = this;
			ZWJSBridge.onReady(() => {
				ZWJSBridge.getLocation()
				    .then((result) => {
				    console.log(result,'浙里办获取定位信息');
				}).catch((error) => {
				    console.log(error);
				});
			});
			
			uni.getLocation({
				type: 'gcj02', //返回可以用于uni.openLocation的经纬度
				geocode: true,
				isHighAccuracy:true,
				success:  (res)=> {
					console.log("获取定位信息")
					that.$http.get("zhdt/check", {
						longitude: res.longitude,
						latitude: res.latitude,
						centerId: that.$centerId
					}).then(res => {
						// 1：可以取号，0不可取号
						// if(res.data.success){
							console.log(res,res.data.data == 1,"-----------------------")
							that.submitFlag = res.data.data == 1;
							that.tipTxt = res.data.message;
						// }else{
						// 	this.submitFlag = false;
						// 	uni.showToast({ title: res.data.message, icon: 'none' });
						// }
						
					})
				},
			});
		},
		getHistoryList(){
			this.$http.get("zhdt/my-order-list",{phone: this.userInfo.phone,centerId: this.$centerId}).then(res=>{
				this.historyList = res.data.data.map(i=>{
					let num = i.isDismiss == 1 ? '3' : i.hasTake == 1 ? "1" : "2";
					if(num == 2){
						if(i && i.overtime == 2) num = 4
					};
					i['statusNum'] = num;
					return i
				})
			})
		},
		reserveCancel(item){
			// if (!this.submitFlag) return;
			this.item = item;
			this.dialogCom = true
		},
		dialogConfirm(){
			// 弹框确认
			this.$http.get("zhdt/cancel-order", {id: this.item.id, phone: this.userInfo.phone,centerId: this.$centerId}).then(res => {
				if (res.data.success) {
					this.dialogCom = false
					this.getHistoryList()
				}else{
					uni.showToast({ title: res.data.message, icon: 'none' })
					this.dialogCom = false
				}
			})
		},
		dialogClose(){
			this.dialogCom = false
		},
		getNumber(item){
			if (!this.submitFlag) return;
			if (item.overtime == 0) {
				uni.showToast({ title: '未到预约时间段内', icon: 'none' })
				return
			};
			this.$http.post("zhdt/take", {
				queueNumber: item.queueNumber,
				qhTag: '1',
				idCard : this.userInfo.idCard,
				idEntity: item.preCallCode,
				phone: this.userInfo.phone,
				centerId: this.$centerId
			}).then(res => {
				// 1：可以取号，0不可取号
				console.log(res)
				if (res.data.success) {
					this.getHistoryList()
				}else{
					uni.showToast({ title: res.data.message, icon: 'none' })
				}
			})
		},
		backPage(){
			uni.reLaunch({ url: "/pages/home/index" })
		},
	}
}
</script>

<template>
	<view class="page-index">
		<view class="dialog-component" v-if="dialogCom">
			<view class="dialog-mw">
				<view class="header">取消预约</view>
				<view class="content">确认取消该预约？</view>
				<view class="footer">
					<button type="default" class="btn1" @click="dialogClose">关闭</button>
					<button type="primary" class="btn2"  @click="dialogConfirm">确认</button>
				</view>
			</view>
			<view class="shade" @click="dialogClose"></view>
		</view>
		<!-- <view class="title"> -->
			<!-- <uni-icons class="icon-back" @click="backPage()" type="back" :size="24" color="#e8feff"></uni-icons> -->
			<!-- <text>我的历史</text> -->
		<!-- </view> -->
		<view class="warn" v-if="!!tipTxt">
			<image :src="img1" mode=""></image>
			<text>{{tipTxt}}</text>
		</view>
		<view class="history-list" v-if="!!historyList.length">
			<view class="item" v-for="(items, index) in historyList" :key="index">
				<view class="item-status" :class="statusData[items.statusNum-1].idClass">
					{{ statusData[items.statusNum-1].title }}
				</view>
				<view class="item-title">{{items.queueName}}</view>
				<view class="item-info" v-if='!!items.code'>
					<text class="label">票号</text>
					<text class="value">{{items.code}}</text>
				</view>
				<view class="item-info" v-if='!!items.takeDate'>
					<text class="label">取号时间</text>
					<text class="value">{{items.takeDate}}</text>
				</view>
				<view class="item-info" v-if='!!items.preCallCode'>
					<text class="label">预约编码</text>
					<text class="value">{{items.preCallCode}}</text>
				</view>
				<view class="item-info" v-if='!!items.preDate'>
					<text class="label">预约时间</text>
					<text class="value">{{`${items.preDate} ${items.beginTime}-${items.endTime}`}}</text>
				</view>
				<view class="item-info">
					<text class="label">预约地点</text>
					<text class="value">{{items.address}}</text>
				</view>
				<!-- overtime  0：时间没到，1，可以取号，2：超时  -->
				<view class="item-footer" v-if="items.statusNum == 2 && items.overtime != 2">
					<view class="left" @click="reserveCancel(items)"> 取消预约</view>
					<view class="right" @click="getNumber(items)" :class='!submitFlag ? "noNum-background" : items.overtime == 0 ? "noNum-background" : ""'>取号 </view>
				</view>
			</view>
		</view>
		<view v-else class="null-data">
			<image :src="img2" mode=""></image>
			<text>暂无数据</text>
		</view>
	</view>
</template>

<style scoped lang="scss">
.page-index{
	color: #3c3c3c;
	min-height: 100vh;
	background-color: #efeff4;
	.dialog-component{
		position: fixed;
		left: 0;
		top: 0;
		z-index: 100;
		.dialog-mw{
			$footerHeight: 80rpx; //声明变量
			.footer{
				position: absolute;
				left: 0;
				bottom: 0;
				width: 100%;
				height: $footerHeight;
				display: flex;
				.btn1{
					border-radius: 0 0 0 20rpx;
				}
				.btn2{
					border-radius: 0 0 20rpx 0;
				}
				button{
					flex: 1;
					height: $footerHeight;
					line-height: $footerHeight;
					&:after{
						border: none;
					}
				}
			}
			.header{
				text-align: center;
				font-size: 32rpx;
			}
			.content{
				margin: 30rpx 0;
				font-size: 28rpx;
				text-align: center;
			}
			position: fixed;
			left: 50%;
			top: 50%;
			z-index: 50;
			transform: translate(-50%,-50%);
			width: 60vw;
			background-color: #fff;
			padding: 40rpx 40rpx $footerHeight;
			border-radius: 20rpx;
			box-shadow: 0 0 20rpx #d0bcbc;
		}
		.shade{
			position: absolute;
			left: 0;
			top: 0;
			width: 100vw;
			height: 100vh;
			z-index: 40;
			background-color: rgba(0, 0, 0, 0.35);
		}
	}
	.history-list{
		box-sizing: border-box;
		padding: 20rpx;
		.item{
			box-sizing: border-box;
			position: relative;
			background-color: #fff;
			border-radius: 16rpx;
			margin-bottom: 20rpx;
			padding-top: 10rpx;
			.item-footer{
				box-sizing: border-box;
				height: 100rpx;
				line-height: 100rpx;
				display: flex;
				flex-flow: row;
				justify-content: space-between;
				border-top: 1px solid #f1f1f1;
				.right{
					box-sizing: border-box;
					flex: 1;
					background-color: #295db1;
					color: #fff;
					font-size: 32rpx;
					text-align: center;
					border-radius: 0 0 16rpx 0;
				}
				.left{
					box-sizing: border-box;
					width: 50%;
					background-color: #b12953;
					color: #fff;
					font-size: 32rpx;
					text-align: center;
					border-radius: 0 0 0 16rpx;
				}
			}
			.item-info{
				box-sizing: border-box;
				display: flex;
				flex-flow: row;
				margin: 20rpx 0 0;
				padding-bottom: 20rpx;
				padding-left: 20rpx;
				.label{
					width: 200rpx;
					font-size: 28rpx;
					color: #999999;
				}
				.value{
					font-size: 28rpx;
					color: #333;
					flex: 1;
				}
			}
			.item-title{
				width: calc(100% - 230rpx);
				padding-left: 20rpx;
				font-size: 28rpx;
				display: inline-block;
				font-weight: bold;
				font-family: PingFang SC-Bold, PingFang SC;
				margin: 10rpx 0;
			}
			.item-status{
				position: absolute;
				right: 0;
				top: 0;
				font-size: 28rpx;
				padding: 10rpx 40rpx;
				border-radius: 0 16rpx 0 16rpx;
			}
			.back-edit{
				background-color: #f5f5f5;
				color: #9f9f9f;
			}
			.back-overtime{
				background-color: #dcf8f8;
				color: #b82628;
			}
			.back-success{
				background-color: #eaf8e7;
				color: #64b84b;
			}
			.back-error{
				background-color: #e0e3e0;
				color: #999999;
			}
		}
	}
	.title{
		width: 100%;
		height: 120rpx;
		line-height: 120rpx;
		text-align: center;
		background-color: #3379b5;
		font-size: 28rpx;
		color: #fff;
		position: relative;
		.icon-back{
			position: absolute;
			left: 30rpx;
			top: 50%;
			transform: translateY(-50%);
		}
	}
}
</style>

